﻿using CommunityToolkit.Mvvm.ComponentModel;
using IPC.DbMapper;
using IPC.WinForm.Bussiness;
using IPC.ListConnect;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IPC.LogCommon.log4net;
using static System.Net.WebRequestMethods;
using LiveChartsCore.Kernel;

namespace IPC.WinForm.ViewModel
{
    public class ViewModel : ObservableObject
    {
        private readonly object _lock = new object();
        public ObservableCollection<ISeries> Series { get; set; }
        private Dictionary<string, ObservableCollection<DateTimePoint>> _observableValues; //ObservableCollection
        
        public ViewModel(Dictionary<string, List<BsonDocument>>  bsonElements)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            _observableValues = new Dictionary<string, ObservableCollection<DateTimePoint>>();
            foreach (var bsonElement in bsonElements)
            {
                string name = bsonElement.Key;
                foreach (var item in bsonElement.Value)
                {
                    double value = item.GetValue("Value").ToString().ToDouble(0);
                    string recordTime = item.GetValue("RecordTime").ToString();

                    DateTime dateTime = DateTime.Now;
                    bool b = DateTime.TryParse(recordTime, out dateTime);
                    if (b)
                    {
                        if (_observableValues.ContainsKey(name))
                        {
                            _observableValues[name].Add(new DateTimePoint(dateTime, value));
                        }
                        else
                        {
                            _observableValues.Add(name, new ObservableCollection<DateTimePoint>());
                            _observableValues[name].Add(new DateTimePoint(dateTime, value));
                        }
                    }
                }
            }


            Series = new ObservableCollection<ISeries>();
            
            foreach (var item in _observableValues)
            {
                Tag tag = DbBussiness.Instance.GetTag(item.Key);
                LineSeries<DateTimePoint> lineSeries = new LineSeries<DateTimePoint>
                {
                    
                    Values = item.Value,
                    Fill = null,
                    Name = tag.Remark,
                    Tag = tag.Remark,
                    GeometrySize = 1,
                };
                
                //lineSeries.XToolTipLabelFormatter = (chartPoint) => $"{new DateTime((long)chartPoint.SecondaryValue):HH:mm:ss}";
                Series.Add(lineSeries);
            }
            //Series.Add()
            //{
            //    new LineSeries<ObservableValue>
            //    {
            //        Values = _observableValues,
            //        Fill = null
            //    }
            //};
        }
        
        public void RemoveItem(string fullName)
        {
            try
            {
                if (_observableValues.ContainsKey(fullName))
                {
                    if (_observableValues[fullName].Count <= 0) return;
                    _observableValues[fullName].RemoveAt(0);
                }
            }
            catch(Exception ex)
            {
                LogClass.Instance.Error($"趋势图删除点错误{ex.Message}");
            }
        }

        public void AddItem(string fullName,string recordTime, string value)
        {
            lock (_observableValues)
            {
                try
                {
                    if (_observableValues.ContainsKey(fullName))
                    {
                        DateTime dateTime = DateTime.Now;
                        bool b = DateTime.TryParse(recordTime, out dateTime);
                        if (b)
                        {
                            _observableValues[fullName].Add(new DateTimePoint(dateTime, value.ToDouble(0)));
                            if (_observableValues[fullName].Count > 1000)
                            {
                                RemoveItem(fullName);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogClass.Instance.Error($"趋势图刷新错误{ex.Message}");
                }
            }       
        }
    }
}
